﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
/// <summary>
/// 吸收激光炮
/// </summary>
public class AbsorptionLaser : Laser {

	public GameObject tempTarget;
	private bool effectting=false;
	
	void Update()
	{
		Attacking ();
	}

	void Attacking()
	{
		// 刷新targetScale
		if(target != null)
		{
			targetScale = target.transform.localScale.x;
			if(target.transform.parent != null)
			{
				targetScale = target.transform.parent.transform.localScale.x * targetScale;
			}
			if(target.transform.Find("Shield(Clone)") != null)
			{
				target = target.transform.Find("Shield(Clone)").gameObject;
				targetScale = target.transform.localScale.x * targetScale;
			}
		}
		// 持续时间到了
		timer += Time.deltaTime;
		durationTime = 5f;
		if(timer > durationTime)
		{
			timer = 0;
			isAttack = false;
			gun.GetComponent<Gun>().isShow = isAttack;
			Destroy(gameObject);
			return;
		}
		// 目标死了
		if(target == null)
		{
			timer = 0;
			isAttack = false;
			gun.GetComponent<Gun>().isShow = isAttack;
			//			interlockPos = Vector3.zero;
			isInterlock = false;
			Destroy(gameObject);
			return;
		}
		if(target != null)
		{
			gun.GetComponent<Gun>().isShow = isAttack;
			distance = Vector3.Distance(transform.position, target.transform.position) - target.GetComponent<CircleCollider2D>().radius * target.transform.localScale.x;

			endPosition = GetEndPosition();
			if(distance <= attackDistance)
			{
				lineRenderer.SetPosition (0, transform.parent.position + new Vector3 (0, 0, -1));
				lineRenderer.SetPosition(1, endPosition);

				if (isAttack) {
					// 渲染射线，攻击目标
					tempTarget = target;
					lineRenderer.enabled = true;
					SetEspecial (gun.transform.parent.gameObject,1);
					MakeDamage ();
				}
			}
			// 超出攻击范围
			else
			{
				// lineRenderer.enabled = false;
				timer = 0;
				isAttack = false;
				target = null;
				gun.GetComponent<Gun>().isShow = isAttack;
				Destroy(gameObject);
			}
		}
	}
	/// <summary>
	/// 附加吸收效果
	/// </summary>
	/// <param name="flag">If set to <c>true</c> flag.</param>
	/// <param name="tempTarget">Temp target.</param>
	void SetEspecial(GameObject gameObject,float recoverHP)
	{
		if (especialEffectId!=0&&target!=null) 
		{
			EspecialEffect.GetInstance ().Absorption (gameObject,recoverHP);
		}
	}
}
